<template>
  <div :class="'chart'" :style="{ height: '100%', width: '300px' }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import { debounce } from "@/utils";

export default {
  // props:["data","dataType"],
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart();
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener("resize", this.__resizeHandler);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    window.removeEventListener("resize", this.__resizeHandler);
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      const pieDataText = this.$store.state.deal_message.totalInfo.typePercent.map(
        (e) => {
          return e.typeName;
        }
      );
      const pieDataPercent = this.$store.state.deal_message.totalInfo.typePercent.map(
        (e) => {
          return { value: e.totals, name: e.typeName };
        }
      );
      this.chart.setOption({
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          orient: "vertical",
          y: "center",
          right: -10,
          data: pieDataText, //['机械设备', '电气设备', '车床', '起重设备', '其他设备']
        },
        calculable: true,
        series: [
          {
            name: "设备分类",
            type: "pie",
            radius: ["50%", "70%"],
            center: ["40%", "50%"],
            avoidLabelOverlap: false,
            left: 0,
            label: {
              show: false,
              position: "center",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "15",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: pieDataPercent,
          },
        ],
      });
    },
  },
};
</script>
